var express = require('express');
var router = express.Router();

var multiparty = require('multiparty');

var fs = require('fs');
const { throws } = require('assert');

router.get('/',function(req,res){
    console.log(__dirname);
    res.sendFile(__dirname.substring(0,__dirname.lastIndexOf('\\')) + './views/index.html');
});

router.post('/',function(req,res){
    var form = new multiparty.Form();

    form.encoding = 'utf-8';
    form.zjtDir = './public/files';
    form.maxFileSize = 20 * 1024 *1024;

    form.parse(req,function(err,fields,files){
        //var filesTemp = JSON.stringify(files,null,2);

        if(err) throw err;

        var zjtFile = files.zjtFile[0];
        var zjtPath = zjtFile.path;

        var dstPath = './public/files' + zjtFile.originalFilename;

        fs.rename(zjtPath,dstPath,(err)=>{
            if(err) throw err;
        });

        res.end();
    });
});

module.exports = router;